#include "dominion_helpers.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rngs.h"
#include <math.h>


//Random test Mine
int randomtestcard(){

	int i, test_val, test_val2, h, num_of_test, test_num, success_count;
	int t_copper_hand_int, t_silver_hand_int, t_copper_hand_count, t_silver_hand_count, t_gold_hand_count;
	int t_copper_hand_count_after, t_silver_hand_count_after, t_gold_hand_count_after;
	int rand_select, which_used, card_fail, test_count;
	int gold_first;
	int b;
	int mine_count;
	int seed = 1000;
	int numPlayer = 2;
	int testhand_count1, testhand_count2;
	int testbuys1, testbuys2;
	int handPos, choice1, choice2, choice3;
	int bonus;
	int k[10] = {adventurer, great_hall, feast, gardens, mine, remodel, smithy, council_room, baron, village};
	struct gameState G;
	int maxHandCount = 10;
	SelectStream(4);
	PutSeed(5);
	srand(8);
	num_of_test = 500000;


	

	memset(&G, 0, sizeof(struct gameState));
	test_val = initializeGame(numPlayer, k, seed, &G);
	test_val = numHandCards(&G);




	testhand_count1 = 15;
	test_val = G.handCount[0];
	success_count = 0;
	card_fail = 0;
	
	for(test_num = 1; test_num <= num_of_test; test_num++){
		srand(test_num);
		test_count += 1;
		initializeGame(numPlayer, k, seed, &G);
		if(test_count > 20){
			
			test_count = 0;
		}
		mine_count = 0;
		handPos = 0;
		rand_select = 0;
		b = 0;
		gold_first = 0;
		t_copper_hand_int = 0;
		t_copper_hand_count = 0;
		t_copper_hand_count_after = 0;
		t_silver_hand_int = 0;
		t_silver_hand_count = 0;
		t_silver_hand_count_after = 0;
		t_gold_hand_count = 0;
		t_gold_hand_count_after = 0;
		G.handCount[0] = testhand_count1;
		G.deckCount[0] = 20;
		G.discardCount[0] = 0;
		which_used = 0;
		for (h = 0; h < G.handCount[0]; h++){
			G.hand[0][h] = rand() % 26;
			if (h == testhand_count1 - 2 && mine_count == 0){
				G.hand[0][h] = 11;
				mine_count += 1;
				handPos = h;
			}
			if(G.hand[0][h] == 4){
				if(t_copper_hand_int == 0)
					t_copper_hand_int = h;
				t_copper_hand_count += 1;
			}
			else if(G.hand[0][h] == 5){
				if(t_silver_hand_int == 0)
					t_silver_hand_int = h;
				t_silver_hand_count += 1;
			}
			else if(G.hand[0][h] == 6)
				t_gold_hand_count += 1;
			else if(G.hand[0][h] == 11){
				mine_count += 1;
				if(handPos == 0)
					handPos = h;
			}
		}
		for (h = 0; h < G.deckCount[0]; h++){
			G.deck[0][h] = rand() % 26;
		}

		//printf("Treasures. Hand %d, Deck %d\n", t_count_hand_int, t_count_deck_int);
		// if(test_num == 147 || test_num == 99 || test_num == 100){
		// 	printf("Card Before is:");
		// 	for (h = 0; h < G.handCount[0]; h++){
		// 		printf("%d, ", G.hand[0][h]);
				
		// 	}
		// 	printf("\nt_copper_count %d t_silver_count %d HandPos is %d t_silver_int %d t_copper_int %d\n", t_copper_hand_count, t_silver_hand_count, handPos, t_silver_hand_int, t_copper_hand_int);
		// }
		// if(test_num == 148){
		// 	printf("Card Before is:");
		// 	for (h = 0; h < G.handCount[0]; h++){
		// 		printf("%d, ", G.hand[0][h]);
				
		// 	}
		// 	printf("\nt_copper_count %d t_silver_count %d HandPos is %d t_silver_int %d t_copper_int %d\n", t_copper_hand_count, t_silver_hand_count, handPos, t_silver_hand_int, t_copper_hand_int);
		// }
		// if(test_num == 150){
		// 	printf("Card Before is:");
		// 	for (h = 0; h < G.handCount[0]; h++){
		// 		printf("%d, ", G.hand[0][h]);
				
		// 	}
		// 	printf("\nt_copper_count %d t_silver_count %d HandPos is %d t_silver_int %d t_copper_int %d\n", t_copper_hand_count, t_silver_hand_count, handPos, t_silver_hand_int, t_copper_hand_int);
		// }
		//printf("handPos is %d Mine Count is %d\n", handPos, mine_count);
		//if(G.hand[0][0] == 5){
		//	gold_first = 1;
		//	printf("\ngold or silver is first\n");
		//	printf("Card Before is:");
		//		for (h = 0; h < G.handCount[0]; h++){
		//			printf("%d, ", G.hand[0][h]);
		//		
		//		}
		//		printf("\nt_copper_hand_count %d t_silver_hand_count %d\n", t_copper_hand_count, t_silver_hand_count);
		//}
		if(t_copper_hand_int != 0 && t_silver_hand_int != 0){
			rand_select = rand() % 2;
			if(rand_select == 0){
				//printf("Card Before is:");
				//for (h = 0; h < G.handCount[0]; h++){
				//	printf("%d, ", G.hand[0][h]);
				//
				//}
				//printf("\nt_copper_hand_count %d t_silver_hand_count %d\n", t_copper_hand_count, t_silver_hand_count);
				

				//printf("testing copper1 %d\n", t_copper_hand_int);
				b = cardEffect(k[4], t_copper_hand_int, 5, choice3, &G, handPos, 0);
				which_used = 1;
			}
			else{
				//printf("testing silver2 %d\n", t_silver_hand_int);
				b = cardEffect(k[4], t_silver_hand_int, 6, choice3, &G, handPos, 0);
				which_used = 2;
				
			}
		}
		else if (t_copper_hand_int != 0){

			
			//printf("Card Before is:");
			//for (h = 0; h < G.handCount[0]; h++){
			//	printf("%d, ", G.hand[0][h]);
			//	
			//}
			//printf("\nt_copper_hand_count %d t_silver_hand_count %d\n", t_copper_hand_count, t_silver_hand_count);
			//printf("testing copper2 %d\n", t_copper_hand_int);
			b = cardEffect(k[4], t_copper_hand_int, 5, choice3, &G, handPos, &bonus);
			which_used = 1;
			
		}
		else if (t_silver_hand_int != 0){

			//printf("testing silver1 %d\n", t_silver_hand_int);
			b = cardEffect(k[4], t_silver_hand_int, 6, choice3, &G, handPos, &bonus);
			which_used = 2;
			
		}
		if(gold_first != 0){
			//printf("Card After is:");
			//		for (h = 0; h < G.handCount[0]; h++){
			//			printf("%d, ", G.hand[0][h]);
			//		
			//		}
			//		printf("\n");
		}

		//Counting up treasures now
		for (h = 0; h < G.handCount[0]; h++){
			if(G.hand[0][h] == 4){
				t_copper_hand_count_after += 1;
			}
			else if(G.hand[0][h] == 5){
				t_silver_hand_count_after += 1;
			}
			else if(G.hand[0][h] == 6)
				t_gold_hand_count_after += 1;
		}
		if(which_used == 2){
			if(t_gold_hand_count_after == t_gold_hand_count + 1 && t_silver_hand_count_after == t_silver_hand_count - 1){
				success_count += 1;
				//printf("purchased gold\n");
			}
			else{
					printf("Card After is:");
					for (h = 0; h < G.handCount[0]; h++){
						printf("%d, ", G.hand[0][h]);
					
					}
					printf("\n");

				printf("Test %d Failed Gold before %d after %d, Silver before %d after %d\n\n", test_num, t_gold_hand_count, t_gold_hand_count_after, t_silver_hand_count, t_silver_hand_count_after);
			}
		}
		else if(which_used == 1){
			if(t_silver_hand_count_after == t_silver_hand_count + 1 && t_copper_hand_count_after == t_copper_hand_count - 1){
				success_count += 1;
				//printf("purchased silver\n");
			}
			else{
					printf("Card After is:");
					for (h = 0; h < G.handCount[0]; h++){
						printf("%d, ", G.hand[0][h]);
					
					}
					printf("\n");
				printf("Test %d Failed Silver before %d  after %d, Copper before %d after %d\n\n", test_num, t_silver_hand_count, t_silver_hand_count_after, t_copper_hand_count, t_copper_hand_count_after);
			}
		}
		else{
		//	printf("which used is %d\n", which_used);
			card_fail += 1;
		}
		if(b != 0)
			printf("hey\n");

	}
	if(success_count > 0)
		printf("%d out of %d successful\n", success_count, num_of_test - card_fail);
	if(card_fail > 0)
		printf("%d out of %d tests failed to have treasure cards in hand\n", card_fail, num_of_test);



	return 0;
}

int main(int argc, char *argv[]){

	randomtestcard();
}